package com.codeoss.framework.workflow.parser;

import org.springframework.stereotype.Service;

import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.workflow.def.EndNodeDef;
import com.codeoss.framework.workflow.def.WorkflowElementDef;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Service
public class EndNodeParser implements IWorkflowElementParser {

	@Override
	public WorkflowElementDef parse(IWorkflowElementParseContext workflowElementParseContext, ObjectNode objectNode) {
		// TODO Auto-generated method stub
		EndNodeDef endNode = new EndNodeDef();
		String id = objectNode.get("id").asText();
		endNode.setId(id);

		JsonNode jsonNode = objectNode.get("startEventExpression");
		if (jsonNode != null) {
			String startEventExpression = jsonNode.asText();
			if (!StringUtils.isEmpty(startEventExpression) && !"null".equals(startEventExpression)) {
				Expression expression = ExpressionUtil.convertToExpression(startEventExpression,
						Expression.EXPRESSION_TYPE_JAVASCRIPT);
				endNode.setStartEventExpression(expression);
			}
		}

		jsonNode = objectNode.get("endEventExpression");
		if (jsonNode != null) {
			String endEventExpression = jsonNode.asText();
			if (!StringUtils.isEmpty(endEventExpression) && !"null".equals(endEventExpression)) {
				Expression expression = ExpressionUtil.convertToExpression(endEventExpression,
						Expression.EXPRESSION_TYPE_JAVASCRIPT);
				endNode.setEndEventExpression(expression);
			}
		}

		return endNode;
	}

}
